package com.hrt.web.member.dao.impl;


import java.util.List;

import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.hrt.base.BaseDaoImpl;
import com.hrt.bean.HRTMember;
import com.hrt.web.member.dao.HRTMemberDao;


@Repository
public class HRTMemberDaoImpl extends BaseDaoImpl<HRTMember> implements HRTMemberDao {
	@Override
	public HRTMember checkHRTMemberOfUnamePwd(final String uname, final String pwd) {
		HRTMember hu = null;
		/*Session session = getSession();
		if (pwd != null) {
			hu = (HRTMember) session.createQuery(
							"FROM HRTMember h where h.mail=? and h.password=?").setString(0, uname).setString(1, pwd).uniqueResult();
		} else {
			List list=this.getHibernateTemplate().find("select h from HRTMember h where h.mail='"+uname+"'");
			System.out.println(list==null);
			if(list!=null && list.size()>0){
			hu = (HRTMember) list.get(0);
			}
		}*/
		String hql = null;
		if(uname.indexOf("@")!=-1){
			 hql = "select a from HRTMember a where a.mail='"+uname+"' and a.password='"+pwd+"' and a.isdelete=0 and a.isMailVaild=1";
		}
		else{
			
			 hql = "select a from HRTMember a where a.mbrName='"+uname+"' or a.mobile='"+uname+"' and a.password='"+pwd+"' and a.isdelete=0";
		}
		List list = getHibernateTemplate().find(hql);
		if(list!=null && list.size()>0){
			hu = (HRTMember) list.get(0);
		}
	
		return hu;
	}
	@Override
	public void addHRTMember(HRTMember hu) {
		getSession().save(hu);
		getSession().flush();
	}
	@Override
	public void updateHRTMemberMailCheck(HRTMember hu) {
		getSession().update(hu);
		getSession().flush();
	}
	@Override
	public HRTMember queryByName(String name) {
		HRTMember  member =null;
		String hql = "from HRTMember a where a.mbrName = '"+name+"'";
		List list = getHibernateTemplate().find(hql);
		if(list!=null&&list.size()>0){
			member =(HRTMember) list.get(0);
		}
		return member;
	}
	@Override
	public HRTMember findMobile(String mobile) {
		HRTMember  member =null;
		String hql = "select a from HRTMember a where a.mobile='"+mobile+"' and a.isdelete=0";
		List list = getHibernateTemplate().find(hql);
		if(list!=null&&list.size()>0){
			member =(HRTMember) list.get(0);
		}
		return member;
	}
	@Override
	public HRTMember getByMail(String mail) {
		HRTMember  member =null;
		String hql = "select a from HRTMember a where a.mail='"+mail+"' and a.isdelete=0";
		List list = getHibernateTemplate().find(hql);
		if(list!=null&&list.size()>0){
			member =(HRTMember) list.get(0);
		}
		return member;
	}
	
	
	
	
	
	
	
	
	
	
	
}
